/*
 * Copyright (c) 2020 - present. All rights reserved.
 *
 * Author : Xianxiang.Hu
 * E-mail : huxianxiang@gmail.com
 * Version: v1.0.0
 * Date   : 2020/01/14 12:55
 * Desc   :
 */

package com.gexiaobao.pigeon.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gexiaobao.pigeon.R;
import com.gexiaobao.pigeon.app.model.bean.GoodsTypeParentResponse;
import com.gexiaobao.pigeon.app.util.ImageLoaderManager;

import java.util.List;

public class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.TopicViewHolder> {

    private final List<GoodsTypeParentResponse.ListBean> mData;
    private OnItemClickListener onItemClickListener;
    private final Context mContext;
    private final int columnCount = 4;

    public TopicAdapter(Context context, List<GoodsTypeParentResponse.ListBean> data) {
        this.mData = data;
        this.mContext = context;
    }

    @Override
    public TopicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TopicViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product_type, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull TopicViewHolder holder, final int position) {
        final GoodsTypeParentResponse.ListBean item = mData.get(position);
        if (item == null) {
            return;
        }
        holder.title.setText(item.getGoodTypeName());
        ImageLoaderManager.INSTANCE.loadRoundImageOrEmpty(mContext, item.getImage(), holder.ivImage, 10, R.mipmap.app_icon_);
        holder.itemView.setOnClickListener(view -> {
            if (onItemClickListener != null) {
                onItemClickListener.onTopicItemClick(position, item);
            }
        });

        GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
        int screenWidth = mContext.getResources().getDisplayMetrics().widthPixels; //屏幕宽度
        params.width = screenWidth / columnCount;
        holder.itemView.setLayoutParams(params);

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    public class TopicViewHolder extends RecyclerView.ViewHolder {

        public AppCompatTextView title;
        public AppCompatImageView ivImage;

        public TopicViewHolder(View view) {
            super(view);
            title = view.findViewById(R.id.title);
            ivImage = view.findViewById(R.id.ivProductTypeImage);
        }
    }

    public interface OnItemClickListener {

        public void onTopicItemClick(Integer position, GoodsTypeParentResponse.ListBean data);

    }

}
